$(function () {
    layui.use(['laydate', 'laypage', 'form'], function () {
            let laypage = layui.laypage;
            //分页完整功能
            laypage.render({
                elem: 'demo7',
                count: getNum(),
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                jump: function (obj) {
                   let pageDatas ={"count":obj.count,"curr":obj.curr,"pages":obj.pages,"limit":obj.limit};

                    $.ajax({
                        url: "http://localhost:8080/back/message/allMessage",
                        dataType: "json",
                        headers:{'username':localStorage.getItem("loginUser")},
                        data: {"page":obj.curr,"limit":obj.limit},
                        success: function (res) {
                            var html;
                            console.log("用户分页返回数据",res)
                            for (var i = 0; i < res.data.length; i++) {
                                html += ' <tr>\n' +
                                    '                                    <td>\n' +
                                    '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
                                    '                                    </td>\n' +
                                    '                                    <td>' + res.data[i].id + '</td>\n' +
                                    '                                    <td>' + res.data[i].subject + '</td>\n' +
                                    '                                    <td>' + res.data[i].email + '</td>\n' +
                                    '                                    <td>' + res.data[i].feedback + '</td>\n' +
                                    '                                    <td class="td-status">\n' +
                                    '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (res.data[i].enable == 1 ? "" : "layui-btn-disabled") + ' ">' + (res.data[i].enable == 1 ? "已回复" : "未回复") + '</span></td>\n' +
                                    '                                    <td class="td-manage">\n' +
                                    '                                      <a title="回复"  onclick="sendInfo('+ res.data[i].id +')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                      <a title="删除" onclick="memberdel(this,' + res.data[i].id + ')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                    </td>\n' +
                                    '                                  </tr>'

                            }
                            $("#userInfo").html(html);

                        }
                    })
                }
            })
        }
    )
})


//得到记录条数
function getNum() {
    let count = 0;
    $.ajax({
        url:"http://localhost:8080/back/message/count",
        dataType:"json",
        headers:{'username':localStorage.getItem("loginUser")},
        async:false,//关闭异步
        success:function (res) {
            count = res.data;

        }
    })
    return count;
}



//编辑普通信息

function sendInfo(id) {
    layui.use(['layer'], function () {
        let layer = layui.layer;
        sessionStorage.setItem("ReplayId", id);

        xadmin.open('回复', '../../html/message/replay.html', 600, 400);

    })
}
//删除某一行用户
function memberdel(obj, id) {
    layui.use(['layer'], function () {
        let message  = {"id":id}
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "http://localhost:8080/back/message/delete",
                type: "post",
                headers:{'username':localStorage.getItem("loginUser")},
                dataType: "json",
                data: JSON.stringify(message),
                contentType:"application/json",
                success: function (res) {
                    if (res.msg == "success") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('删除失败!', {icon: 1, time: 1000});
                    }


                }
            })
        });
    })
}

